872. 叶子相似的树
为保证权益,题目请参考 872. 叶子相似的树(From LeetCode).
解决方案1
Python
python
from typing import List
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def getLayers(root: TreeNode) -> List[int]:
ans = []
def dfs(root: TreeNode):
if root is None:
return
if root.left is None and root.right is None:
ans.append(root.val)
else:
dfs(root.left)
dfs(root.right)
dfs(root)
return ans
class Solution:
def leafSimilar(self, root1: TreeNode, root2: TreeNode) -> bool:
return getLayers(root1) == getLayers(root2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30